home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / PROGEDIT / 0748.ZIP / ISRCH < prev    next >
Text File  |  1986-12-29  |  2KB  |  64 lines

  1. /* This macro performs an EMACS-like incremental search */
  2.  
  3. init()
  4. {
  5.   assign_key("i_search", 19);   /* <CTRL> S invokes the search */
  6. }
  7.  
  8.  
  9. i_search()
  10. {
  11.   int c;
  12.   int len;
  13.   string pattern;
  14.  
  15.   c = 0;             /* initialize everything */
  16.   pattern = "";
  17.   save_position();
  18.  
  19.   while (c != '\E')                       /* <ESC> gets us out */
  20.   {
  21.     message(strcat("Incr Search -- ", pattern));
  22.  
  23.     if ((c = get_tty_char()) == '\b')     /* backspace */
  24.     {
  25.       if ((len = strlen(pattern)) > 1)    /* erase the last char */
  26.         pattern = substr(pattern, 1, len - 1);
  27.       else
  28.         pattern = "";
  29.       restore_position();                 /* go to where we started from */
  30.     }
  31.  
  32.     else if (c == 7)                      /* <CTRL> G means abort */
  33.     {
  34.       for (len = strlen(pattern);  len > 0;  len = len - 1)
  35.         restore_position();               /* restore to start position */
  36.       break;
  37.     }
  38.  
  39.     else if (c == '\n' || c == '\E')      /* done!!! - stay where we are */
  40.       break;
  41.  
  42.     else                                  /* a regular search character */
  43.     {
  44.       /* Notice the use of chr() below. If we didn't use it, then c would */
  45.       /* be converted to a numeric string before it was concatenated. */
  46.       pattern = strcat(pattern, chr(c));  /* tack it onto the end of pattern */
  47.  
  48.       message(strcat("Incr Search -- ", pattern));
  49.       save_position();
  50.  
  51.       if (fsearch(pattern) <= 0)          /* search for the pattern */
  52.       {                                   /* it failed! */
  53.         if ((len = strlen(pattern)) > 1)  /* erase this character */
  54.           pattern = substr(pattern, 1, len - 1);
  55.         else
  56.           pattern = "";
  57.         restore_position();
  58.       }
  59.     }
  60.   }
  61.  
  62.   clear_positions();                       /* reset the position stack */
  63. }
  64.